home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / Apple Script / OSAX / Four Password OSAXen / PassWord3 Folder / PassWord3.c < prev    next >
C/C++ Source or Header  |  1993-05-14  |  5KB  |  180 lines

  1. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. //    Password3.c
  4. //    Written by: Donald Olson
  5. //    May 1993
  6. //    Copyright ® 1993 Apple Computer Inc.
  7. //     All rights reserved.
  8. //
  9. //    This is a simple Scripting Addition that asks the user to type in a
  10. //    password.  This is different from PassWord.c in that the users 
  11. //     password is stored in the refCon field so that it only needs to be 
  12. //    entered once. In addition, this version has an optional parameter
  13. //    that clears the stored password. This was written for a talk at the 
  14. //    1993 WWDC given by Donald Olson and Donn Denman.
  15. //
  16. //    Syntax: User Password3 [Clear]
  17. //    Return: Encrypted form of user password or cancel.
  18. //
  19. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  20.  
  21. #include <string.h>
  22. #include <Dialogs.h>
  23. #include <AppleEvents.h>
  24. #include <Memory.h>
  25.  
  26. // Our headers
  27. void        FrameDefault( DialogPtr theDialog, long theItem);
  28. pascal     Boolean MyDlgFilter(    DialogPtr theDlg, 
  29.                             EventRecord *theEventRec, short *itemHit);
  30. void     EncryptString(StringPtr strPtr);
  31.  
  32. // Our constants
  33. #define DLOGID     128
  34. #define MYClass     'OLIE'
  35. #define MyID        'psw3'
  36. #define OurEnum    'cler'
  37.  
  38. pascal OSErr main(AppleEvent *theEvent, 
  39.                 AppleEvent *theReply, 
  40.                 long theRefCon) {
  41.  
  42.     DialogPtr             theDialog = nil;
  43.     OSErr                theErr = noErr;
  44.     GrafPtr                savedPort;
  45.     short                itemHit;
  46.     Str32                theString;
  47.     StringPtr                theStrPtr = (StringPtr)&theString;
  48.     StringHandle            theStrHdl = (StringHandle)&theStrPtr;
  49.     EventHandlerProcPtr    handler;
  50.     long                    handlerRefcon, actualSize;
  51.     Handle                ourGlobalHdl = nil;
  52.     DescType            typeCode;
  53.     long                    theEnum =  0;
  54.     
  55.     // Get the direct object, if any.
  56.     theErr = AEGetParamPtr(theEvent, keyDirectObject, 
  57.                     typeEnumerated, &typeCode, 
  58.                     (Ptr)&theEnum, sizeof(theEnum), &actualSize);
  59.     
  60.     if(OurEnum == theEnum) {
  61.         if(theRefCon == 0) // We've got no stored password.
  62.             return noErr;
  63.             
  64.         theErr = AEGetEventHandler(MYClass, MyID, 
  65.                                 &handler, &handlerRefcon, true);
  66.         
  67.         DisposeHandle((Handle)handlerRefcon);    // Get rid of our handle
  68.             
  69.         theErr = AEInstallEventHandler(MYClass, MyID, 
  70.                                         handler, 0, true);
  71.         
  72.     }
  73.     
  74.     if(theRefCon != 0) // We've got a stored password.
  75.     goto ADDPARAM;
  76.     
  77.     GetPort(&savedPort);
  78.     
  79.     theErr = AEInteractWithUser(kAEDefaultTimeout, nil, nil);
  80.     if(theErr != noErr) return theErr;
  81.     
  82.     theDialog = GetNewDialog(DLOGID, nil, (WindowPtr)-1);    
  83.         
  84.        if(theDialog == nil) return resNotFound;        
  85.        
  86.         SetPort(theDialog);
  87.     FrameDefault(theDialog, ok);
  88.     
  89.     strcpy((char*)theString, (char*) "\0"); // Empty string
  90.     SetWRefCon(theDialog, (long)theStrHdl);
  91.     
  92.     do {
  93.         ModalDialog((ModalFilterProcPtr)MyDlgFilter, &itemHit);
  94.     } while (itemHit != cancel && itemHit != ok);
  95.      if(itemHit == cancel) {
  96.          DisposDialog(theDialog);
  97.          return userCanceledErr;
  98.      }
  99.     
  100.     DisposDialog(theDialog);
  101.     SetPort(savedPort);
  102.     
  103.     EncryptString(theStrPtr);
  104.     
  105.     theErr = AEGetEventHandler(MYClass, MyID, 
  106.                                 &handler, &handlerRefcon, true);
  107.     
  108.     ourGlobalHdl = NewHandleSysClear(sizeof(theString));
  109.     if(ourGlobalHdl == nil) return MemError();
  110.     
  111.     HLock((Handle)theStrHdl);
  112.     //theErr = HandAndHand((Handle)theStrHdl, ourGlobalHdl);
  113.     BlockMove((Ptr)*theStrHdl, (Ptr)*ourGlobalHdl, sizeof(theString));
  114.     HUnlock((Handle)theStrHdl);
  115.     
  116.     if(theErr != noErr)
  117.         return theErr;
  118.         
  119.     theErr = AEInstallEventHandler(    MYClass, MyID, 
  120.                                 handler, (long)ourGlobalHdl, true);
  121.     
  122.     theRefCon = (long)ourGlobalHdl; // For our local use.
  123.     ADDPARAM:;
  124.     HLock((Handle)theRefCon);
  125.     theErr = AEPutParamPtr(    theReply, keyDirectObject, typeChar, 
  126.                             (Ptr)*(Handle)theRefCon, strlen((char*)*(Handle)theRefCon));
  127.     
  128.     HUnlock((Handle)theRefCon);
  129.     return theErr;
  130. }
  131.  
  132. void EncryptString(StringPtr strPtr) {
  133.     short    theSize, x;
  134.     theSize = strlen((char*)(strPtr));
  135.     
  136.     for( x = 0; x <= (theSize -1); x++) {
  137.         strPtr[x] = (short)strPtr[x] + (short)"\¡";
  138.     }
  139. }
  140.  
  141. pascal Boolean MyDlgFilter(DialogPtr theDlg, 
  142.                         EventRecord *theEventRec, 
  143.                         short *itemHit) 
  144. {        
  145.     StringHandle    theStrHdl;
  146.     short            theSize;
  147.     if(theEventRec->what != keyDown) // Just looking for keystrokes
  148.         return false;
  149.             
  150.     switch((theEventRec->message) & charCodeMask)
  151.     {
  152.         case 0x0d:                // Return
  153.             *itemHit = ok;      
  154.             return true;
  155.         case 0x03:                // Enter
  156.             *itemHit = ok;      
  157.             return true;
  158.         case 0x08:                // Backspace
  159.             return false;
  160.         default:
  161.             theStrHdl = (StringHandle)GetWRefCon(theDlg);
  162.             theSize = strlen((char*)(*theStrHdl));
  163.             (*theStrHdl)[theSize] = (char)(theEventRec->message);
  164.             (*theStrHdl)[theSize + 1] = (char)NULL;
  165.             theEventRec->message = '•';
  166.             return false;
  167.     }
  168. }
  169.  
  170. void FrameDefault( DialogPtr theDialog, long theItem) {
  171.     short    DType; 
  172.     Handle    DItem;
  173.     Rect    DRect;
  174.     
  175.     GetDItem(theDialog, theItem, &DType, &DItem, &DRect); 
  176.     PenSize(3, 3); 
  177.     InsetRect(&DRect, -4, -4);
  178.     FrameRoundRect(&DRect, 16, 16);
  179.     PenSize(1, 1);
  180. }